package com.sz.jinzheng.controller;

import com.sz.jinzheng.client.UserCilent;
import com.sz.jinzheng.model.PageBean;
import com.sz.jinzheng.model.Result;
import com.sz.jinzheng.model.Task;
import com.sz.jinzheng.service.DistributionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author qin
 * @version 1.0
 * @description: TODO
 * @date 2021/7/22 22:53
 */
@Slf4j
@RestController
@RequestMapping("/distribution")
public class DistributionController {
    @Autowired
    private DistributionService distributionService;
    @Autowired
    private UserCilent userCilent;

    //查询所有业务经理已审批通过的客户贷款信息
    @RequestMapping("/getAllTask")
    public Result getAllTask(@RequestBody Task task, int index, int size){
        PageBean<Task> allTask = distributionService.getAllTask(task, index, size);
        return new Result(200,"查询到所有已审批通过的客户贷款信息",allTask);
    }

    //领取任务，修改任务表中的状态信息
    @RequestMapping("/pickTask")
    public Result pickTask(@RequestBody Task task) {
        //通过token拿到uid
        Integer uid = userCilent.getUid(task.getToken());
        task.setUserId(uid);
        log.info("task=" + task);
        //更新任务表中的status为9，表示发放专员已领取
        int i = distributionService.updateTaskStatus(task);
        if (i > 0) {
            return new Result(200, "领取成功", null);
        }
        return new Result(409, "领取失败请稍后重试", null);
    }

    //查询自己待完成的任务
    @RequestMapping("/getMyCompletingTask")
    public Result getMyCompletingTask(@RequestBody Task task, int index, int size) {
        //通过token拿到uid
        Integer uid = userCilent.getUid(task.getToken());
        task.setUserId(uid);
        log.info("task=" + task);
        PageBean<Task> taskPageBean = distributionService.getMyCompletingTask(task, index, size);
        return new Result(200, "查询成功", taskPageBean);
    }

    //查询自己已完成的任务
    @RequestMapping("/getMyCompletedTask")
    public Result getMyCompletedTask(@RequestBody Task task, int index, int size) {
        //通过token拿到uid
        Integer uid = userCilent.getUid(task.getToken());
        task.setUserId(uid);
        log.info("task=" + task);
        PageBean<Task> taskPageBean = distributionService.getMyCompletedTask(task, index, size);
        return new Result(200, "查询成功", taskPageBean);
    }

    //根据申请记录id和领取任务后的tsid，即t_staff_exmination表中的id，查询客户贷款信息
    @RequestMapping("/getDistributionTask")
    public Result getDistributionTask(@RequestBody Task task) {
        log.info("task=" + task);
        Task task1=distributionService.getDistributionTask(task);
//        log.info("task1=" + task1);
        if (task1!=null) {
            return new Result(200, "查询客户贷款信息", task1);
        }
        return new Result(409, "未查询到客户贷款信息", task1);
    }

    //同意或者不同意放款
    @RequestMapping("/loan")
    public Result loan(@RequestBody Task task,Integer status){
        //通过token拿到uid
        Integer uid = userCilent.getUid(task.getToken());
        task.setUserId(uid);
        log.info("task=" + task);

        int i=distributionService.loan(task,status);
        if (i>0){
            return new Result(200,"任务完成");
        }
        return new Result(201,"任务失败");
    }
}
